﻿using System;
using System.Data.Common;

namespace Centido.Management.ConsoleManager
{
    /// <summary>
    ///   A console command that adds the specified user to the specified security role.
    /// </summary>
    internal class AddUserInRoleCommand : Command
    {
        /// <summary>
        ///   The user name parameter.
        /// </summary>
        public const string UserNameParameter = "user";

        /// <summary>
        ///   The user name parameter (short name).
        /// </summary>
        public const string UserNameShortParameter = "u";

        /// <summary>
        ///   The role name parameter.
        /// </summary>
        public const string RoleNameParameter = "role";

        /// <summary>
        ///   The role name parameter (short name).
        /// </summary>
        public const string RoleNameShortParameter = "r";


        /// <summary>
        ///   Gets the console command name.
        /// </summary>
        public override string Name
        {
            get { return "adduserinrole"; }
        }


        /// <summary>
        ///   Gets the console command description.
        /// </summary>
        public override string Description
        {
            get { return Messages.AddUserInRoleCommandDescription; }
        }


        /// <summary>
        ///   Gets the console command full description.
        /// </summary>
        public override string FullDescription
        {
            get { return Messages.AddUserInRoleCommandFullDescription; }
        }


        /// <summary>
        ///   Executes the console command.
        /// </summary>
        /// <param name="context">The console command execution context.</param>
        /// <exception cref="ArgumentNullException">Thrown when the value of <paramref name="context"/> is null.</exception>
        public override void Execute(CommandExecutionContext context)
        {
            base.Execute(context);

            // If the user name or the role name was not specified
            if (!context.ParameterValueIsNotEmpty(UserNameParameter, UserNameShortParameter)
                || !context.ParameterValueIsNotEmpty(RoleNameParameter, RoleNameShortParameter))
            {
                Console.WriteLine(Messages.AddUserInRoleCommandNotAllParametersSpecifiedError);
                return;
            }

            var domain = context.CurrentDomain;

            if (domain == null) return;

            // If the current user has no permission to perform this operation
            if (!domain.CurrentUser.CanChangeSecurity)
            {
                Console.WriteLine(Messages.NeedCanChangeSecurityPermissionMessage);
                return;
            }

            // The user name
            var userName = context.GetParameterValue(UserNameParameter, UserNameShortParameter);

            // The role name
            var roleName = context.GetParameterValue(RoleNameParameter, RoleNameShortParameter);

            try
            {
                // Trying to find the user
                var user = domain.GetUser(userName);

                // If the user was not found
                if (user == null)
                {
                    Console.WriteLine(Messages.CannotFindUserError, userName);
                    return;
                }

                // Trying to find the role
                var role = domain.GetRole(roleName);

                // If the role was not found
                if (role == null)
                {
                    Console.WriteLine(Messages.CannotFindRoleError, roleName);
                    return;
                }

                if (!role.ContainsUser(user))
                    role.AddUser(user);
            }
            catch (DbException e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine();
                return;
            }

            // Displaying the success message
            Console.WriteLine(Messages.CommandCompletedMessage);
        }
    }
}
